package com.mediastory.spsserver.apns.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mediastory.spsserver.apns.domain.Apns;
import com.mediastory.spsserver.apns.service.ApnsSvc;

/**
 * @author Jaewon Choi
 */

@Controller
@RequestMapping(value={"/apns"})
public class ApnsCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private ApnsSvc apnsSvc;

	/**
	 * 목록 보기
	 * */
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> list(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		String gubun = ServletRequestUtils.getStringParameter(request, "gubun");
		String name = ServletRequestUtils.getStringParameter(request, "name");
		String startDate = ServletRequestUtils.getStringParameter(request, "startDate");
		String endDate = ServletRequestUtils.getStringParameter(request, "endDate");
		condition.put("gubun", gubun);
		condition.put("name", name);
		condition.put("startDate", startDate);
		condition.put("endDate", endDate);

		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(apnsSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		return resultMap;
	}

	/**
	 * 등록 폼, APNS 상세 보기
	 * */
	@RequestMapping(value={"/form.do"})
	public Map<String, Object> form(HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		int id = ServletRequestUtils.getIntParameter(request, "id", 0);
		if(id != 0){
			Apns apns = apnsSvc.findby(id);
			resultMap.put("apns", apns);
		}
		
		return resultMap;
	}
	
	/**
	 * APNS 등록/수정
	 * */
	@RequestMapping(value={"/submit.do"}, method=RequestMethod.POST)
	public String submit(Apns apns, HttpServletRequest request) throws Exception {

		if(apns.getId() != null){
			apns.setModifiedId(request.getUserPrincipal().getName());
			apns.setModifiedIp(request.getRemoteAddr());
			apnsSvc.modify(apns);
		}else{
			apns.setCreatedId(request.getUserPrincipal().getName());
			apns.setCreatedIp(request.getRemoteAddr());
			apnsSvc.add(apns);
		}

		return "apns/result";
	}
	
	/**
	 * APNS 삭제
	 * */
	@RequestMapping(value={"/remove.do"}, method=RequestMethod.POST)
	public String remove(HttpServletRequest request) throws Exception {
		
		int[] id = ServletRequestUtils.getIntParameters(request, "id");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		
		condition.put("modifiedId", request.getUserPrincipal().getName());
		condition.put("id", id);
		
		apnsSvc.remove(condition);
		return "apns/result";
	}
	
	/**
	 * 바로 전송
	 * */
	@RequestMapping(method=RequestMethod.GET, value="/sendMsg")
	public @ResponseBody void sendMsg() throws Exception {
		apnsSvc.push();
	}

}